package ScalaStudy.chapter06

object Test06_ConstructorParams {
  def main(args: Array[String]): Unit = {


    /////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////    带参数的构造器  ///////////////////////////////////
    //////////////////////////////////////根据参数进行调用的////////////////////////////////////


    //调用无参的构造器

    val student2 = new Student2
    student2.name = "wq"
    student2.age  = 27
    println(s"student2: name = ${student2.name}, age = ${student2.age}")

    //调用有参的构造器
    val student3 = new Student3("owo", 20)
    println(s"student3: name = ${student3.name}, age = ${student3.age}")

    //调用有参无修饰的构造器
    val student4 = new Student4("wow", 4)
//    println(s"student4: name = ${student4.name}, age = ${student4.age}")         ---error （name和age只是参数）
    student4.printInfo()

    //调用val定义的属性，赋值以后不可做修改
    val student5 = new Student5("oop", 5)
        println(s"student5: name = ${student5.name}, age = ${student5.age}")

    //调用主构造器和辅助构造器都有参数的
    val student6 = new Student6("wow", 4)                           //传递两个参数代用主构造器
    println(s"student6: name = ${student6.name}, age = ${student6.age}")
    student6.printInfo

    val student66 = new Student6("wow", 4,"合肥工业大学")       //传递三个参数，调用主和辅助构造器
    println(s"student7: name = ${student66.name}, age = ${student66.age}")
    student66.printInfo



  }
}



//无参构造器
class Student2 {
  //单独定义属性
  var name:String = _
  var age: Int = _
}

//上面定义等价于*******************最好的写法*******************
class Student3(var name:String, var age: Int)        //定义一个有参的构造器，其中构造器中有name,age两个属性

//主构造器参数无修饰
class Student4(name:String, age: Int){               //此时name和age只是参数，不是属性了
  def printInfo(): Unit ={
    println(s"student4: name = ${name}, age = ${age}")
  }
}

//不推荐以下写法
class Student44(_name:String, _age: Int){
  var name: String = _name
  var age : Int    = _age
}

//主构造器参数无修饰
class Student5(val name:String, val age: Int)        //定义val的属性

//主构造器和辅助构造器都有参数
class Student6(var name:String, var age: Int) {
  var school:String = _

  def this(name: String, age: Int, school: String){
    this(name, age)                                   //调用主构造器
    this.school = school
  }

  def printInfo(): Unit ={
    println(s"student46 name = ${name}, age = ${age}, school = ${school}")
  }

}